#! /usr/bin/env python
#
#
#*******************************************************************************
# saga_cmd_param_parser.py
#
# (c) 2011 by Volker Wichmann
# contact: wichmann@laserdata.at
#
# this script lists all parameter settings of the SAGA module which is passed
# as argument
#*******************************************************************************

import saga_api, sys, os

#*******************************************************************************
def print_param(p):
    name  = p.Get_Name()
    desc  = p.Get_Description(saga_api.PARAMETER_DESCRIPTION_TYPE).c_str()
    type  = p.Get_Type_Name()
    # you can also get type as ESG_Parameter_Type by p.Get_Type()
    ident = p.Get_Identifier()
    
    print '%s\t \t%s \t%s \t-%s' % (name, desc, type, ident)

    return 1

#*******************************************************************************
def append_call_syntax(call, p):
    type  = p.Get_Type_Name()
    ident = p.Get_Identifier()

    call = call + ' -' + ident + ' <' + type + '>'

    return call

#*******************************************************************************
def list_param_module(fmlb, mname):

    mlb     = saga_api.CSG_Module_Library()

    if mlb.Create(saga_api.CSG_String(fmlb)) == 0:
        print 'ERROR loading ' + fmlb
        return 0
    m       = mlb.Get_Module(mname)
    if m is None:
        print 'Unable to find module ' + fmlb
        return 0

    ps = m.Get_Parameters()

    call = 'saga_cmd ' + '"' + mname + '"'

	# parse module type

    type = m.Get_Type()

    print '\nModule Type:'

    if type == saga_api.MODULE_TYPE_Base:
        print '\nBase'
    elif type == saga_api.MODULE_TYPE_Interactive:
        print '\nBase interactive: unable to execute interactive modules with saga_cmd'
        return 0
    elif type == saga_api.MODULE_TYPE_Grid:
        print '\nGrid'
        m = mlb.Get_Module_Grid(mname)
    elif type == saga_api.MODULE_TYPE_Grid_Interactive:
        print '\nGrid interactive: unable to execute interactive modules with saga_cmd'
        return 0
    else:
        print '\nERROR: unknown module type'
        return 0

    # parse input parameters
    bFirst = True
    for i in range(0, ps.Get_Count()):
        p = ps.Get_Parameter(i)
        if p.is_Input():
            if bFirst:
                bFirst = False
                print '\nParameters Input:'

            print_param(p)
            call = append_call_syntax(call, p)

    # parse output parameters
    bFirst = True
    for i in range(0, ps.Get_Count()):
        p = ps.Get_Parameter(i)
        if p.is_Output():
            if bFirst:
                bFirst = False
                print '\nParameters Output:'

            print_param(p)
            call = append_call_syntax(call, p)

    # parse option parameters
    bFirst = True
    for i in range(0, ps.Get_Count()):
        p = ps.Get_Parameter(i)
        if p.is_Option():
            if bFirst:
                bFirst = False
                print '\nParameters Option:'

            print_param(p)
            call = append_call_syntax(call, p)

	# print saga_cmd call

    print '\n\n' + call

    return 1


#*******************************************************************************
